package com.umpay.util;

import com.umpay.dict.retcode.MerBusiEC;
import com.umpay.exception.BusinessException;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName: bankCodeConvertUtil  
 * @Description: 银行简码转换工具类
 * @author zhangtao  
 * @date Feb 6, 2018
 */
public class BankCodeConvertUtil {
	private static final Logger log = LogManager.getLogger(BankCodeConvertUtil.class);
	
	/**
	 * @Title: convertGateId2BinbankId  
	 * @Description: 将gateId转换成binbankId
	 * @param @param gateId
	 * @param @return    参数  
	 * @return String    返回类型  
	 * @throws
	 */
	public static String convertGateId2BinbankId(String gateId) {
		PropertyUtil propertyUtil = (PropertyUtil)SpringUtil.getBean("propertyUtil");
		String bankInfo = propertyUtil.getProperty("b2b.bankcode.mapping");// 配置文件取出新的映射关系
		log.info("配置的联动简码->银行简码映射关系" + bankInfo);
		Map<String, String> banksMap = new HashMap<String, String>();
		if(StringUtils.isNotEmpty(bankInfo)){
	        String[] banks = bankInfo.split(";");
	        if(banks != null && banks.length > 0){
	          for (String bank : banks){
	              String[] bankmap = bank.split(",");
	                  if (bankmap != null && bankmap.length > 1){
	                      banksMap.put(bankmap[0], bankmap[1]);
	               }
	            }
	        }
       }  
	   String binBankId = banksMap.get(gateId);
	   if(StringUtils.isEmpty(binBankId)){
	       throw new BusinessException(MerBusiEC.CARD_NOT_SUPPORTED, "很抱歉，该银行卡不支持绑卡操作");
	   }
	   return binBankId;
	}
	/**
	 * @Title: convertBinbankId2GateId  
	 * @Description: 将binbankId转换成 gateId
	 * @param @param binbankId
	 * @param @return    参数  
	 * @return String    返回类型  
	 * @throws
	 */
	public static String convertBinbankId2GateId(String binbankId) {
		PropertyUtil propertyUtil = (PropertyUtil)SpringUtil.getBean("propertyUtil");
		String bankInfo = propertyUtil.getProperty("b2b.bankcode.mapping");// 配置文件取出新的映射关系
		log.info("配置的联动简码->银行简码映射关系" + bankInfo);
		Map<String, String> banksMap = new HashMap<String, String>();
		if (StringUtils.isNotEmpty(bankInfo)){
	        String[] banks = bankInfo.split(";");
	        if(banks != null && banks.length > 0){
	          for (String bank : banks){
	              String[] bankmap = bank.split(",");
	                  if (bankmap != null && bankmap.length > 1){
	                      banksMap.put(bankmap[1], bankmap[0]);
	                }
	            }
	        }
       }  
	   String gateId = banksMap.get(binbankId);
	   if(StringUtils.isEmpty(gateId)){
	       throw new BusinessException(MerBusiEC.CARD_NOT_SUPPORTED, "很抱歉，该银行卡不支持绑卡操作");
	   }
	   return gateId;
	}
	
	
	
	
	/**
	 * @Title: convertGateId2BinbankId  
	 * @Description: 将gateId转换成binbankId
	 * @param @param gateId
	 * @param @return    参数  
	 * @return String    返回类型  
	 * @throws
	 */
	public static String convertFMGateId2BinbankId(String gateId) {
		PropertyUtil propertyUtil = (PropertyUtil)SpringUtil.getBean("propertyUtil");
		String bankInfo = propertyUtil.getProperty("cqfmb.bankcode.mapping");// 配置文件取出新的映射关系
		log.info("配置的联动简码->银行简码映射关系" + bankInfo);
		Map<String, String> banksMap = new HashMap<String, String>();
		if(StringUtils.isNotEmpty(bankInfo)){
	        String[] banks = bankInfo.split(";");
	        if(banks != null && banks.length > 0){
	          for (String bank : banks){
	              String[] bankmap = bank.split(",");
	                  if (bankmap != null && bankmap.length > 1){
	                      banksMap.put(bankmap[0], bankmap[1]);
	               }
	            }
	        }
       }  
	   String binBankId = banksMap.get(gateId);
	   if(StringUtils.isEmpty(binBankId)){
	       throw new BusinessException(MerBusiEC.CARD_NOT_SUPPORTED, "很抱歉，该银行卡不支持绑卡操作");
	   }
	   return binBankId;
	}
	/**
	 * @Title: convertBinbankId2GateId  
	 * @Description: 将binbankId转换成 gateId
	 * @param @param binbankId
	 * @param @return    参数  
	 * @return String    返回类型  
	 * @throws
	 */
	public static String convertFMBinbankId2GateId(String binbankId) {
		PropertyUtil propertyUtil = (PropertyUtil)SpringUtil.getBean("propertyUtil");
		String bankInfo = propertyUtil.getProperty("cqfmb.bankcode.mapping");// 配置文件取出新的映射关系
		log.info("配置的联动简码->银行简码映射关系" + bankInfo);
		Map<String, String> banksMap = new HashMap<String, String>();
		if (StringUtils.isNotEmpty(bankInfo)){
	        String[] banks = bankInfo.split(";");
	        if(banks != null && banks.length > 0){
	          for (String bank : banks){
	              String[] bankmap = bank.split(",");
	                  if (bankmap != null && bankmap.length > 1){
	                      banksMap.put(bankmap[1], bankmap[0]);
	                }
	            }
	        }
       }  
	   String gateId = banksMap.get(binbankId);
	   if(StringUtils.isEmpty(gateId)){
	       throw new BusinessException(MerBusiEC.CARD_NOT_SUPPORTED, "很抱歉，该银行卡不支持绑卡操作");
	   }
	   return gateId;
	}
}
